<?php

namespace App\Kuafu\CouponPackage\Controller\B;

use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Activity\Exceptions\ActivityException;
use App\Kuafu\Activity\Validate\ActivityValidate;
use App\Kuafu\Activity\Enum\ActivityCodeEnum;
use App\Kuafu\Activity\Service\ActivityService;
use App\Kuafu\CouponPackage\Enum\CouponPackageCodeEnum;
use App\Kuafu\CouponPackage\Service\CouponPackageService;
use App\Kuafu\CouponPackage\Validate\CouponPackageMappingValidate;
use App\Kuafu\CouponPackage\Validate\CouponPackageShopMappingValidate;
use App\Kuafu\CouponPackage\Validate\CouponPackageValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class CouponPackageController extends BaseController
{

    /**
     * 创建优惠券包
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Kuafu\CouponPackage\Exceptions\CouponPackageException
     */
    public function create(Request $request): JsonResponse
    {
        list(
            $couponPackageParams,
            $couponPackageMappingParams,
            $couponPackageShopMappingParams,
            $errMsg
            ) = CouponPackageValidate::couponPackageInsertValidate($request->all());
        if ($errMsg) {
            return $this->fail(CouponPackageCodeEnum::ERR_COUPON_PACKAGE_PARAM_VALID, $errMsg);
        }

        CouponPackageService::create(
            $request->header('brandId'),
            $couponPackageParams,
            $couponPackageMappingParams,
            $couponPackageShopMappingParams,
            auth('api')->user()->id
        );
        return $this->success([]);
    }


    /**
     * 更新优惠券包
     *
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, int $id): JsonResponse
    {
        list(
            $couponPackageParams,
            $couponPackageMappingParams,
            $couponPackageShopMappingParams,
            $errMsg
            ) = CouponPackageValidate::couponPackageInsertValidate($request->all());
        if ($errMsg) {
            return $this->fail(CouponPackageCodeEnum::ERR_COUPON_PACKAGE_PARAM_VALID, $errMsg);
        }

        CouponPackageService::update(
            $request->header('brandId'),
            $id,
            $couponPackageParams,
            $couponPackageMappingParams,
            $couponPackageShopMappingParams,
            auth('api')->user()->id
        );
        return $this->success([]);
    }

    /**
     * 优惠券包列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function lists(Request $request): JsonResponse
    {
        $name            = (string)$request->get('name', '');
        $status          = $request->get('status', '');
        $shopIds         = $request->get('shopIds', 0);
        $createdUserName = $request->get('createdUserName', '');
        $pageSize        = $request->get('pageSize', 20);
        $pageNum         = $request->get('pageNum', 1);
        return $this->success(CouponPackageService::lists($pageNum, $pageSize, $request->header('brandId'), $name,
            $status, $shopIds, $createdUserName));
    }


    /**
     * 获取行记录
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws \App\Kuafu\CouponPackage\Exceptions\CouponPackageException
     */
    public function row(Request $request, int $id): JsonResponse
    {
        $row = CouponPackageService::row($id);
        return $this->success(['row' => $row]);
    }

    /**
     * 设置状态
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ActivityException
     */
    public function setStatus(Request $request): JsonResponse
    {
        $status = (int)$request->get('status', 0);
        $id     = (int)$request->get('id', 0);
        CouponPackageService::setStatus($id, $status);
        return $this->success();
    }

    //获取券包销售明细
    public function getCouponPackageSalesDetail(Request $request)
    {
        //编号  名称   手机号   销售门店   销卡时间
        $name            = $request->get('name', 0);
        $endAt           = $request->get('endAt', 0);
        $phone           = $request->get('phone', 0);
        $shopId          = $request->get('shopId', 0);
        $startAt         = $request->get('startAt', 0);
        $couponPackageId = $request->get('couponPackageId', 0);
        $pageNum         = (int)$request->get('pageNum', 1);
        $pageSize        = (int)$request->get('pageSize', 20);

        if (empty($shopId)) {
            return $this->fail(CouponPackageCodeEnum::ERR_COUPON_PACKAGE_PARAM_VALID, '请选择门店');
        }

        $list = CouponPackageService::packageDetail($couponPackageId, $name, $phone, $shopId, $startAt, $endAt,
            $pageNum, $pageSize);

        return $this->success($list);
    }

    //导出明细
    public function exportDetail(Request $request)
    {
        //编号  名称   手机号   销售门店   销卡时间
        $name            = $request->get('name', 0);
        $endAt           = $request->get('endAt', 0);
        $phone           = $request->get('phone', 0);
        $shopId          = $request->get('shopId', 0);
        $startAt         = $request->get('startAt', 0);
        $couponPackageId = $request->get('couponPackageId', 0);
        $tmpFilePath     = CouponPackageService::detailExport($couponPackageId, $name, $phone, $shopId, $startAt,
            $endAt);
        return (new BinaryFileResponse($tmpFilePath, 200, [
            'Content-Disposition' => 'attachment;filename=' . urlencode(basename($tmpFilePath)),
            'Content-Type'        => 'application/octet-stream',
        ]))->deleteFileAfterSend();
    }
}
